package com.bytedance.common.utility.d;

import android.content.Context;
import android.os.Build;
import android.util.Log;

import com.bytedance.common.utility.reflect.JavaCalls;

import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public final class SoLibraryLoader {
    private static List<String> mLoadedLibraryNameList = new ArrayList();

    private static String a(Context context, String libName, File soFile) {
        InputStream v2_4;
        FileOutputStream v2_3 = null;
        InputStream v3_3 = null;
        String v0_3;
        int v0_2;
        ZipEntry v0_1;
        ZipFile v3;
        Closeable v2;
        Closeable v3_1;
        ZipFile v4 = null;
        Closeable v1 = null;
        try {
            v4 = new ZipFile(new File(context.getApplicationInfo().sourceDir), 1);
            v0_1 = v4.getEntry("lib/" + Build.CPU_ABI + "/" + System.mapLibraryName(libName));
            if (v0_1 == null) {
                v0_2 = Build.CPU_ABI.indexOf(45);
                StringBuilder v2_1 = new StringBuilder("lib/");
                String v3_2 = Build.CPU_ABI;
                if (v0_2 <= 0) {
                    v0_2 = Build.CPU_ABI.length();
                }
                String v2_2 = v2_1.append(v3_2.substring(0, v0_2)).append("/").append(System.mapLibraryName(libName)).toString();
                v0_1 = v4.getEntry(v2_2);
                if (v0_1 != null) {
                } else {
                    v0_3 = "Library entry not found:" + v2_2;
                    android.support.design.a.closeQuietly(v1);
                    android.support.design.a.closeQuietly(v1);
                    android.support.design.a.a(v4);
                    return v0_3;
                }
            }
            soFile.createNewFile();
            v3_3 = v4.getInputStream(v0_1);
            v2_3 = new FileOutputStream(soFile);
            v0_2 = 16384;
            byte[] v0_4 = new byte[v0_2];
            while (true) {
                int v5 = v3_3.read(v0_4);
                if (v5 <= 0) {
                    break;
                }
                v2_3.write(v0_4, 0, v5);
            }
            JavaCalls.a("android.os.FileUtils", soFile.getAbsolutePath(), new Object[]{Integer.valueOf(493), Integer.valueOf(-1), Integer.valueOf(-1)});
            android.support.design.a.closeQuietly(v2_3);
            android.support.design.a.closeQuietly(v3_3);
            android.support.design.a.a(v4);
            return null;
        } catch (Throwable v0) {
            return v0.getMessage();
        } finally {
            android.support.design.a.closeQuietly(v2_3);
            android.support.design.a.closeQuietly(v3_3);
            android.support.design.a.a(v4);
        }
    }

    public static synchronized boolean loadLibrary(Context context, String libName) {
        try {
            if (!mLoadedLibraryNameList.contains(libName)) {
                System.loadLibrary(libName);
                mLoadedLibraryNameList.add(libName);
            }
            return true;
        } catch (UnsatisfiedLinkError unsatisfiedLinkError) {
            String systemLibName = System.mapLibraryName(libName);
            File soDir;
            if (context == null || context.getFilesDir() == null) {
                soDir = null;
            } else {
                soDir = new File(context.getFilesDir(), "libso");
                if (!soDir.exists()) {
                    new File(soDir.getAbsolutePath()).mkdirs();
                }
            }

            File soFile = null;
            if (soDir != null) {
                soFile = new File(soDir, systemLibName);
            }

            if (soFile == null) {
                return false;
            }

            if (soFile.exists()) {
                soFile.delete();
            }
            String v4_1 = a(context, libName, soFile);
            if (v4_1 != null) {
                Log.e("loadLibrary", unsatisfiedLinkError.getMessage() + "[" + v4_1 + "]");
                return false;
            }

            try {
                System.load(soFile.getAbsolutePath());
                mLoadedLibraryNameList.add(libName);
                return true;
            } catch (Throwable throwable) {
                return false;
            }
        }
    }
}

